// <!-- GrowingIO Analytics code version 0.1 -->
// <!-- Copyright 2015-2017 GrowingIO, Inc. More info available at http://www.growingio.com -->
// {/* <script type='text/javascript'> */}

/* eslint-disable */

import md5 from 'blueimp-md5';
import axios from 'axios';
import encrypt from '@core/EncryptProvider';


var EventTrack = {
  init: function (env, extConfig = {}, origin) {
    this.mode = '';
    if (!window.gdp) {
      if (extConfig.platform == 'hybrid') {
        this.mode = 'hybrid';
        !function (e, t, n, g, i) {
          let tag = null;
          e[i] = e[i] || function () {
            (e[i].q = e[i].q || []).push(arguments)
          }, n = t.createElement("script"), tag = t.getElementsByTagName("script")[0], n.async = 1, n.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + g, tag.parentNode.insertBefore(n, tag)
        }(window, document, "script", "assets.giocdn.com/cdp/hybrid/gio_hybrid_cdp.js", "gio");
      } else {
        this.mode = 'js';
        !function (e, t, n, g, i) {
          let tag = null;
          e[i] = e[i] || function () {
            (e[i].q = e[i].q || []).push(arguments)
          }, n = t.createElement("script"), tag = t.getElementsByTagName("script")[0], n.async = 1, n.src = g, tag.parentNode.insertBefore(n, tag)
        }(window, document, "script", "https://assets.giocdn.com/cdp/1.0/gio.js", "gdp");
      }
    }

    // 埋点环境配置
    var accountId, datasourceId, host, debug
    if (origin === 'agent') {
      //custom page code begin here
      // A端
      accountId = env.VUE_APP_ENV == 'production' ? '111' : '222'
      datasourceId = env.VUE_APP_ENV == 'production' ? '111' : '222'
    } else {
      //custom page code begin here
      // 默认值：目前是C端在使用
      accountId = env.VUE_APP_ENV == 'production' ? '111' : '222'
      datasourceId = env.VUE_APP_ENV == 'production' ? '111' : '222'
    }
    // 埋点公用配置
    host = env.VUE_APP_ENV == 'production' ? 'gio-data-test.baidu.com' : 'gio-data-test.baidu.com'
    debug = env.VUE_APP_ENV == 'production' ? false : true


    let config = {
      host: host,
      debug: debug,
      ...extConfig
    }

    if (this.mode == 'hybrid') {
      this.useGio('init', accountId, config);
    }
    else {
      this.useGio('init', accountId, datasourceId, config)
    }

    //custom page code end here
    this.useGio('send');

    this.service = axios.create({
      baseURL: '/',
      withCredentials: true, // 跨域请求发送cookie
      timeout: 60000 // 请求超时时间
    });

    this.service.interceptors.request.use(function (config) {
      if (env.VUE_APP_ENCRYPT === 'true') {
        console.log('请求加密->：', config.data);
        config = encrypt.encryptConfig(config);
      }
      return config;
    }, function (error) {
      return Promise.reject(error);
    });

    this.service.interceptors.response.use(function (response) {
      // 解密
      if (env.VUE_APP_ENCRYPT === 'true') {
        response = encrypt.encryptResponse(response);
        console.log('返回解密->：', response.data);
      }
      return response.data;
    }, function (error) {
      return Promise.reject(error);
    });

    this.$http = async function () {
      return this.service({
        url: '/app-gateway/ucs-consumer/userInfo/getUserIdAndType',
        method: 'post',
        data: {}
      });
    }
  },
  install: function (Vue) {
    let userId = undefined;
    let _this = this;
    Vue.prototype.$initGioUserId = async function () {
      console.log('初始化userId:', userId);
      if (userId) return;
      try {
        const res = await _this.$http();
        console.log('获取userId：', res);
        if (res.code == 200 && res.data.userId) {
          userId = md5(res.data.userId);
          console.log('加密userId：', userId);
          _this.useGio('setUserId', userId);
          // gdp('setUserId', userId)
        }
      } catch (error) {
        console.log('%c[gio-warning]', 'color: #f00', '获取userId异常!');
      }
    }
  },
  useGio: function () {
    if (this.mode == 'hybrid') {
      return gio.apply(this, arguments);
    }
    else {
      return gdp.apply(this, arguments);
    }
  }
};

export default EventTrack;

// window.gdp('track', 'dietPageView') // dietPageView-饮食计划 sportPageView-运动计划

// </script>
// <!-- End GrowingIO Analytics code version: 0.1 -->
